#include "actionfindwindow.h"
#include <QtCore>
#include <Windows.h>
#include <QtGui>

ActionFindWindow::ActionFindWindow()
{
    classname = __FUNCTION__;
    actParam = QJsonObject();
}

ExecuteRet ActionFindWindow::execute()
{
    int width = actParam.value("width").toInt(0);
    int height = actParam.value("height").toInt(0);
    QString className = actParam.value("class").toString();
    QString title = actParam.value("title").toString();
    ActionWhenFind action = (ActionWhenFind)(actParam.value("action").toInt());
    HWND lastFound = NULL;
    HWND found = NULL;
    qInfo()<<classname<<"find window"<<title<<"class:"<<className<<"size:"<<width<<"x"<<height;
    wchar_t *pTitle = NULL,*pClass = NULL;
    if(title.length()){
        pTitle = new wchar_t[title.length()+5];
        pTitle[title.toWCharArray(pTitle)] = 0;
    }
    if(className.length()){
        pClass = new wchar_t[className.length()+5];
        pClass[className.toWCharArray(pClass)] = 0;

    }
    do
    {
        found = ::FindWindowExW(NULL,lastFound,pClass,pTitle);
        if(found == NULL){
            qInfo()<<"not found"<<className<<"-"<<title;
            return ERSub1;
        }

        if(TRUE == ::IsWindowVisible(found)){
            //char windowTitle[100]={0};
            //GetWindowTextA(found,windowTitle,100);
            //qDebug()<<"windowTitle:"<<windowTitle;
            RECT rect;
            GetWindowRect(found,&rect);
            if((width==0&&height==0) || (width == rect.right-rect.left &&
                    height == rect.bottom - rect.top))
            {
                actResult.insert("x",rect.left);
                actResult.insert("y",rect.top);
                actResult.insert("width",rect.right-rect.left);
                actResult.insert("height",rect.bottom-rect.top);
                BOOL actionResult = FALSE;
                switch(action)
                {
                case AWRaise:
                    actionResult = SetForegroundWindow(found);
                    //BringWindowToTop(found);
                    break;
                case AWClose:
                    ::SendMessage(found,WM_CLOSE,0,0);
                    //actionResult = DestroyWindow(found);
                    break;
                case AWHide:
                    actionResult = ShowWindow(found,SW_HIDE);
                    break;
                case AWMini:
                    actionResult = ShowWindow(found,SW_MINIMIZE);
                    break;
                default:
                    break;
                }

                return ERSucc;
            }
        }
        lastFound = found;
    }
    while(found);

    return ERSub1;
}

void ActionFindWindow::matchSize(int w, int h)
{
    actParam.insert("width",w);
    actParam.insert("height",h);
}

void ActionFindWindow::matchClass(QString className)
{
    actParam.insert("class",className);
}

void ActionFindWindow::matchTitle(QString title)
{
    actParam.insert("title",title);
}

void ActionFindWindow::setAction(ActionWhenFind action)
{
    actParam.insert("action",(int)action);
}
